//
//  BHStadiumOrderDetailViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/6.
//

import UIKit

class BHStadiumOrderDetailViewController: BHBaseViewController {

    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .grouped)
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight)
        view.delegate = self
        view.dataSource = self
        view.isHidden = true
        return view
    }()
    
    var orderId: String = ""
    
    fileprivate var model = BHUserOrderDetailModel.init()
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.title_label.text = "订单详情"

        self.regisetCell()
        self.view.addSubview(self.tableView)
        
        self.bh_navigationBar.rightBtn.isHidden = false
        self.bh_navigationBar.rightBtn.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
        self.bh_navigationBar.rightBtn.setTitleColor(.color_D43939, for: .normal)
        self.queryOrderDetail()
    }
    
    override func rightBtnAction() {
        if self.model.refundStatus == 0 { // 未退款
            BHCommonTool.showCustomAlertWith(showVC: self, title: "提示", message: "是否确定要取消订单？", okBtn: "再想想", cancel: "现在取消") { (cancel) in
                if cancel == false {
                    self.cancelOrderService()
                }
            }
        }else {
            BHToast.toast(text: "功能开发中，敬请期待")
        }
    }
    

    private func regisetCell()  {
        self.tableView.regiseterNib(nibName: "BHStadiumOrderInfoHeaderCell")
        self.tableView.regiseterNib(nibName: "BHStadiumDetailPlaceInfoCell")
        self.tableView.regiseterNib(nibName: "BHStadiumOrderPayInfoCell")
        self.tableView.regiseterNib(nibName: "BHCoachRefundDescriptionCell")
    }

}
extension BHStadiumOrderDetailViewController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return BHStadiumOrderInfoHeaderCell.cellHeight
        }else if (indexPath.section == 1) {
            return BHStadiumDetailPlaceInfoCell.cellHeight
        }else if indexPath.section == 2 {
            if self.model.status != 1 {
                return 0
            }
            return 53
        }else {
            return 130
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.leastNormalMagnitude
        }else {
            if self.model.status != 1 && section == 2{
                return CGFloat.leastNormalMagnitude
            }else {
                return 10
            }
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
}

extension BHStadiumOrderDetailViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            return nil
        }
        if section == 2 && self.model.status != 1 {
            return nil
        }
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 10))
        view.backgroundColor = .color_f8f8f8
        return view
    }

 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHStadiumOrderInfoHeaderCell", for: indexPath) as! BHStadiumOrderInfoHeaderCell
            cell.model = self.model
            return cell
        }else if indexPath.section == 1{
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHStadiumDetailPlaceInfoCell", for: indexPath) as! BHStadiumDetailPlaceInfoCell
            cell.model = self.model
            cell.goStadiumDetailBtn.addBtnAction(action: {[weak self] (_) in
                if self?.model.venueId.isEmpty == false {
                    BHRouter.shared.bh_goStadiumDetail(stadium: self?.model.venueId ?? "")
                }
            }, for: .touchUpInside)
            return cell
        }else if indexPath.section == 2{
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHCoachRefundDescriptionCell", for: indexPath) as! BHCoachRefundDescriptionCell
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHStadiumOrderPayInfoCell", for: indexPath) as! BHStadiumOrderPayInfoCell
            cell.model = self.model

            return cell
        }
    }
}


extension BHStadiumOrderDetailViewController {
    func queryOrderDetail()  {
        SVProgressHUD.show()
        let manager = BHHTTPClient.init()
        manager.get(url: String.order_myOrder_detail_url, paramters: ["id":self.orderId]) { (success, errorMsg, responseDic) in
            SVProgressHUD.dismiss()
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                self.model.parseDic(result)
                self.tableView.isHidden = false
                self.tableView.reloadData()
                
                if  self.model.refundFlag == 1 {
                    self.bh_navigationBar.rightBtn.isHidden = false
                    if self.model.refundStatus == 0 { // 未退款
                        self.bh_navigationBar.rightBtn.setTitle("取消订单", for: .normal)
                    }else {
                        self.bh_navigationBar.rightBtn.setTitle("删除", for: .normal)
                    }
                    self.bh_navigationBar.rightBtn.setImage(nil, for: .normal)
                }else {
                    self.bh_navigationBar.rightBtn.isHidden = true
                }
               
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
    
    func cancelOrderService()  {
        SVProgressHUD.show()
        let manager = BHHTTPClient.init()
        manager.get(url: String.order_cancel_myOrder_url, paramters: ["id":self.orderId]) { (success, errorMsg, responseDic) in
            SVProgressHUD.dismiss()
            if success == true {
                BHToast.toast(text: "退款成功")
                DispatchQueue.main.asyncAfter(deadline: .now()+1.5) {
                    self.navigationController?.popViewController(animated: true)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
}
